package com.gdones.gpt.bus.notice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyPager;
import com.gdones.common.core.dto.MyResult;
import com.gdones.gpt.bus.gptstaff.dto.Staff;
import com.gdones.gpt.bus.gptstaff.mapper.StaffMapper;
import com.gdones.gpt.bus.modes.dto.GptModes;
import com.gdones.gpt.bus.notice.dto.GptNotice;
import com.gdones.gpt.bus.notice.mapper.GptNoticeMapper;
import com.gdones.gpt.bus.notice.service.IGptNoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-06-16
 */
@Service
@Transactional
public class GptNoticeServiceImpl extends ServiceImpl<GptNoticeMapper, GptNotice> implements IGptNoticeService {

    @Autowired
    private GptNoticeMapper gptNoticeMapper;

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String myMailPath;

    @Autowired
    private StaffMapper staffMapper;

    @Override
    public MyResult pages(GptNotice dto, MyPageBand pageBand) {

        MyResult result = new MyResult();

        IPage<GptNotice> pages = gptNoticeMapper.pages(PageDTO.of(pageBand.getPage(), pageBand.getSize()), dto);

        // 根据分页的结果封装返回数据
        MyPager<GptNotice> pager = new MyPager<>();
        pager.setTotal(pages.getTotal());
        pager.setPage(pageBand.getPage());
        pager.setPageSize(pageBand.getSize());
        pager.setRows(pages.getRecords());

        result.setData(pager);
        result.setMsg("查询成功！");

        return result;
    }

    @Override
    public MyResult mylist(GptNotice dto) {

        MyResult result = new MyResult();

        List<GptNotice> list = gptNoticeMapper.myList(dto);

        result.setData(list);
        result.setMsg("查询成功！");

        return result;
    }

    @Override
    public MyResult getByID(String id) {

        MyResult result = new MyResult();

        GptNotice byId = this.getById(id);
        if(byId==null){
            result.setMsg("查询id不存在！");
            result.setCode("9999");
            return result;
        }

        result.setData(byId);

        return result;
    }

    @Override
    public MyResult add(GptNotice dto) {

        MyResult result = new MyResult();

        boolean save = this.save(dto);
        if(save){
            result.setMsg("添加成功！");
        }else{
            result.setMsg("添加失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult update(GptNotice dto) {

        MyResult result = new MyResult();

        boolean save = this.updateById(dto);
        if(save){
            result.setMsg("修改成功！");
        }else{
            result.setMsg("修改失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult deletes(String ids) {

        MyResult result = new MyResult();

        List<String> idList = Arrays.asList(ids.split(","));

        boolean b = this.removeBatchByIds(idList);
        if(b){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult sendEmail(String id) throws MessagingException {

        MyResult result = new MyResult();

        GptNotice byId = this.getById(id);
        if(byId == null){
         result.setMsg("要发送的通知不存在！");
         result.setData("9999");
         return result;
        }

        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        List<Staff> staff = staffMapper.selectList(queryWrapper);

        for(Staff tempStaff : staff){
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(myMailPath);
            helper.setTo(tempStaff.getUserName());
            helper.setSubject(byId.getName());
            helper.setText(byId.getNoticeDesc(), true);
            mailSender.send(message);
        }

        byId.setIsSendEmail("1");
        boolean b = this.updateById(byId);

        result.setMsg("发送完成！");

        return result;
    }

    @Override
    public MyResult updateToTc(String id) {

        MyResult result = new MyResult();

        // 先将其他通知都设置为不弹窗
        int i = gptNoticeMapper.updateAllNoTc();

        // 将当前id设置
        GptNotice updateDo = new GptNotice();
        updateDo.setNoticeID(id);
        updateDo.setIsTc("1");

        boolean save = this.updateById(updateDo);
        if(save){
            result.setMsg("设置成功！");
        }else{
            result.setMsg("设置失败！");
            result.setCode("9999");
        }

        return result;
    }
}
